package mobile

import (
	"go-toy/app/model"
	"go-toy/app/service"
	"go-toy/constant/response"
	"go-toy/database/orm"
	"go-toy/util"

	"github.com/gin-gonic/gin"

	Controller "go-toy/app/controller"
)

type SpiderController struct {
	Controller.Common
}

// Defines the request parameters format
var QueryParamsSpiderList = map[string]Controller.QueryParamsStruct{
	"keyword": {
		KeyType: "string",
	},
	"source": {
		KeyType:    "many",
		KeyDefault: 1,
		KeyOptions: []any{1, 2, 3},
	},
}

/**
 * @api {get} /mobile/spiders Get Spiders
 */
func (s *SpiderController) List(gc *gin.Context) {
	// Set the request parameters format for the List API
	s.QueryParams = QueryParamsSpiderList

	// Initialize the Common struct
	s.Initialize(gc)

	// Initialize the Spider service
	spiderService := service.Spider{}

	// Retrieve the list of spiders
	result, total, err := spiderService.List(s.PageOffset, s.PageLimit, s.OrderBy, []string{}, s.Conditions)
	if err != nil {
		util.Response.ResponseFailure(gc, response.CODE_FAILURE, err.Error())
		return
	}
	s.SuccessListResult(gc, result, total)
}

func (s *SpiderController) Info(gc *gin.Context) {
	var spider model.Spider
	orm.DB.First(&spider)
	util.Response.ResponseSuccess(gc, gin.H{"info": spider})
}

// Singleton instance of SpiderController
var SpiderControllerinstance *SpiderController = &SpiderController{}
